package org.gdpi.ssh.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.gdpi.ssh.entitys.Grade;
import org.gdpi.ssh.server.GradeServer;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class GradeAction extends ActionSupport implements ModelDriven<Grade>{

	private static final long serialVersionUID = 1L;
	private Grade formGrade = new Grade();
	public Grade getFormGrade() {
		return formGrade;
	}
	@Override
	public Grade getModel() {
		return formGrade;
	}
	private GradeServer gradeServer;
	public void setGradeServer(GradeServer gradeServer) {
		this.gradeServer = gradeServer;
	}


	public String list(){
		List<Grade> grades = gradeServer.findAll();
		ServletActionContext.getRequest().setAttribute("gradeList", grades);
		return "grade_list.jsp";
	}
	
	
	public String toEdit(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Integer editId = Integer.parseInt(request.getParameter("graId"));
		Grade editGrade = gradeServer.get(editId);
		request.setAttribute("editGrade", editGrade);
		return "grade_edit.jsp";
	}
	
	public String edit(){
		gradeServer.update(formGrade);
		return "edit";
	}
	
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Integer deleteId = Integer.parseInt(request.getParameter("graId"));
		Grade deleteGrade = new Grade();
		deleteGrade.setId(deleteId);
		gradeServer.delete(deleteGrade);
		return "delete";
	}

	public String checkDelete(){
		String checkId = ServletActionContext.getRequest().getParameter("deleteId");
		formGrade = gradeServer.get(Integer.parseInt(checkId));
		return "checkDelete";
	}
	
	public String toAdd(){
		return "grade_add.jsp";
	}
	
	
	public String add(){
		gradeServer.add(formGrade);
		return "add";
	}
}
